# 广度搜索 
from collections import deque

direct = [[-1, 0], [0, -1], [0, 1], [1, 0]]
def bfs(grid, visited, x, y):
    queue = deque()
    queue.append([x,y])
    
    while queue:
        cur_x, cur_y = queue.popleft()
        
        for i,j in direct:
            next_x = cur_x + i
            next_y = cur_y + j
            
            if next_x < 0 or next_x >= len(grid[0]) or next_y < 0 or next_y >= len(grid):
                continue
            
            if grid[next_y][next_x] == 1 and not visited[next_y][next_x]:
                visited[next_y][next_x] = True
                queue.append([next_x,next_y])

if __name__ == '__main__':
    
    row, col = map(int, input().split())
    
    grid = []
    for i in range(row):
        grid.append(list(map(int, input().split())))
        
    visited = [[False]*col for i in range(row)]
    
    res = 0
    for i in range(row):
        for j in range(col):
            
            if grid[i][j] == 1 and not visited[i][j]:
                visited[i][j] = True
                res += 1
                bfs(grid, visited, j, i)
    print(res)